/* Import plugin specific language pack */
tinyMCE.importPluginLanguagePack('dwcode');

var TinyMCE_DWCodePlugin = {
	getInfo: function() {
		return {
			longname: 'DW Code Plugin',
			author: 'David Walsh',
			authorurl: 'http://davidwalsh.name',
			infourl: 'http://davidwalsh.name',
			//version: '.10'
			version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
		};
	},
	
	//Gets executed when a TinyMCE editor instance is initialized.
	initInstance: function(inst) {
		
	},
	
	//Gets executed when a TinyMCE editor instance is removed.
	removeInstance: function(inst) {
		
	},
	
	//Gets executed when a TinyMCE editor instance is displayed using for example mceToggleEditor command.
	showInstance: function(inst) {
		
	},
	
	//Gets executed when a TinyMCE editor instance is hidden using for example mceToggleEditor command.
	hideInstance: function(inst) {
		
	},
	
	//Returns the HTML code for a specific control or empty string if this plugin doesn't have that control.
	//A control can be a button, select list or any other HTML item to present in the TinyMCE user interface.
	//The variable {$editor_id} will be replaced with the current editor instance id and {$pluginurl} will be replaced
	//with the URL of the plugin. Language variables such as {$lang_somekey} will also be replaced with contents from
	//the language packs.
	getControlHTML : function(cn) {
		switch (cn) {
			case 'dwcode':
				return tinyMCE.getButtonHTML(cn, 'lang_dwcode_button', '{$pluginurl}/images/dw.gif', '_launchWindow');
		}

		return '';
	},

	
	//ets called ones the cursor/selection in a TinyMCE instance changes. This is useful to enable/disable.
	handleNodeChange: function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
		//alert(node.tagName);
	},
	
	//Gets called when a TinyMCE editor instance gets filled with content on startup.
	setupContent: function(editor_id, body, doc) {
		
	},
	
	//Gets called when the contents of a TinyMCE area is modified, in other words when a undo level is added.
	onChange: function(inst) {
		
	},
	
	//Gets called when TinyMCE handles events such as keydown, mousedown etc.
	handleEvent: function(e) {
		return true;
	},
	
	//Gets called when HTML contents is inserted or retrived from a TinyMCE editor instance.
	cleanup: function(type, content, inst) {
		return content;
	},
	
	execCommand: function(editor_id, element, command, user_interface, value) {
		
		switch(command) {
			case '_launchWindow':
				
				//window stuff
				wind = new Array();
				wind['file'] = '../../plugins/dwcode/editor.htm';
				wind['width'] = 700;
				wind['height'] = 500;
				wind['inline'] = 'yes';
				wind['resizable'] = 'yes';
				wind['close_previous'] = "yes";
				
				//add more to window stuff?
				//get the focused element
				var default_content = '';
				var inst = tinyMCE.getInstanceById(editor_id), focusElm = inst.getFocusElement();
				if (focusElm != null && focusElm.nodeName == "PRE") 
				{ 
					default_content = focusElm.innerHTML;
				}
				
				tinyMCE.openWindow(wind, { 
					editor_id: editor_id,
					default_content: default_content,
					replace_node: focusElm,
					pre_class: focusElm.getAttribute('class'),
					inline: 'yes',
					resizable : "yes"
				});
				
				tinyMCE.setWindowArg('mce_windowresize', true);
				
		}
		
	},
	
	/* internal plugin functions here */
	
	
	_launchWindow: function() {
		
		
		
	}
	
};


// Adds the plugin class to the list of available TinyMCE plugins
tinyMCE.addPlugin("dwcode",TinyMCE_DWCodePlugin);
